{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bagging(dir):\n",
    "    pred_name_list = os.listdir(dir)\n",
    "    pred = pd.read_csv(os.path.join('./preds',pred_name_list[0]))\n",
    "    pred_id = pred.id\n",
    "    pred_prob = pred.prob\n",
    "    for pred_name in pred_name_list[1:]:\n",
    "        pred = pd.read_csv(os.path.join('./preds',pred_name))\n",
    "        pred_prob += pred.prob\n",
    "    pred_prob = pred_prob/len(pred_name_list)\n",
    "    pred = pd.DataFrame()\n",
    "    pred['id'] = pred_id\n",
    "    pred['prob'] = pred_prob\n",
    "    return pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred = bagging('./preds')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pred.to_csv('./pred.txt',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0.787    10 folds\n",
    "# 0.799    3 folds\n",
    "# 0.817    1 fold"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
